package com.roc.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.DaoException;
import com.roc.newPage.IPageDao;
import com.roc.pojo.EstrnStastics;
import com.roc.service.EstrnStasticsService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.web.action.estrn.EstrnStasticsSearchCondition;
@Service
public class EstrnStasticsServiceImpl implements EstrnStasticsService{
	
	private Logger logger=LogManager.getLogger(EstrnStasticsServiceImpl.class);
	@Autowired
	private IPageDao pageDao;
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<EstrnStastics> queryEstrnStasticsList(EstrnStasticsSearchCondition searchCondition)
			throws ServiceException {
		List<Object> params = new ArrayList<Object>();
		params.add(Constance.Null2EmptyTrim(searchCondition.getMinDate()));
		params.add(Constance.Null2EmptyTrim(searchCondition.getMaxDate()));
		StringBuffer sb = new StringBuffer();
		sb = sb.append("SELECT SUM(t.trans_at) / 100, t.trans_dt, t.ma_trans_cd ");
		if ("1".equals(Constance.Null2EmptyTrim(searchCondition.getDateFlag()))) {//1 历史表    2临时表
			sb.append("FROM apstfr.tbl_tfr_his_trn_log t ");
		}
		if ("2".equals(Constance.Null2EmptyTrim(searchCondition.getDateFlag()))) {//1 历史表    2临时表
			sb.append("FROM hbat.tbl_tfr_trn_log t ");
		}
		if (!"-1".equals(Constance.Null2EmptyTrim(searchCondition.getAreaId()))) {
			sb.append("left join tbl_mcht_inf tm on t.mchnt_cd=tm.mcht_cd ");
		}
		sb.append("WHERE t.trans_dt >=? ").append(" and t.trans_dt <=? ")
				.append("AND t.resp_cd = '00' ")
				.append("AND t.trans_st = '1000000000' ");
		if ("-1".equals(Constance.Null2EmptyTrim(searchCondition.getAreaId()))) {
			sb.append("AND trim(T.Industry_Ins_Id_Cd) =? ");
			params.add(searchCondition.getOrgCode());
		}
		if (!"-1".equals(Constance.Null2EmptyTrim(searchCondition.getAreaId()))) {
			sb.append("and exists (select 1 from tbl_org_main e where e.area_id=? and e.org_code=tm.uc_bc_cd_area)");
			params.add(searchCondition.getAreaId());
		}

		sb.append("AND trim(T.MA_TRANS_CD) IN ('1011', '1171') ").append(
				"GROUP BY t.trans_dt, t.ma_trans_cd ");

		List<Object> searchRsList = new ArrayList<Object>();
		List<EstrnStastics> estrnStasticList = new ArrayList<EstrnStastics>();
		try {
			searchRsList = pageDao
					.findAllByParams(sb.toString(), params, 0, 20);
			dealTheListSpecilly(estrnStasticList, searchRsList);
		} catch (Exception e) {
			logger.info("交易统计信息查询异常：" + e.getMessage());
		}
		return estrnStasticList;
	}
	
	

	private void dealTheListSpecilly(List<EstrnStastics> estrnStasticList,
			List<Object> searchRsList) {
		if(searchRsList != null && searchRsList.size()>0){
			Iterator it = searchRsList.iterator();
			while(it.hasNext()){
				Object[] obj = (Object[]) it.next();
				EstrnStastics es = new EstrnStastics();
				es.setTotalMoney(Constance.Null2EmptyTrim(obj[0]));
				es.setTransDt(Constance.Null2EmptyTrim(obj[1]));
				String maTransCd = Constance.Null2EmptyTrim(obj[2]);
				es.setMaTransCd(maTransCd);
				if("1011".equals(maTransCd)){
					es.setMaTransCdName("T1");
				}else if("1171".equals(maTransCd)){
					es.setMaTransCdName("T0");
				}else{
					es.setMaTransCdName("未知");
				}
				estrnStasticList.add(es);
			}
		}
	}



	@Override
	public int validationFourElements(String acctNo) throws ServiceException {
		String sql =" SELECT COUNT(*) FROM T_WECHAT_VALIDATE T WHERE T.ACCOUNT_NO=? ";
		int count=0;
		List<Object> parmas=new ArrayList<Object>();
		parmas.add(acctNo);
		try {
			count = pageDao.countByParams(sql, parmas);
			if(count>0){
				logger.info("查询该交易卡号【"+acctNo+"】进行四要素验证次数【"+count+"】次");
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("查询交易卡号四要素验证失败！");
		}
		
		return count;
	}

}
